home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5927 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  86 lines

  1. Path: nntp.atlanta.com!usenet
  2. From: cam03@cppccam03.homedepot.com (Chuck Mattern)
  3. Newsgroups: comp.lang.c
  4. Subject: passing of pointers
  5. Date: 21 Feb 1996 23:47:56 GMT
  6. Organization: Internet Atlanta
  7. Message-ID: <4ggavc$f4u@nntp.atlanta.com>
  8. NNTP-Posting-Host: 151.140.22.53
  9. X-Newsreader: knews 0.9.5
  10.  
  11. I'm stumped here and know this is probably a newbie sort of question
  12. but I'll ask anyway.  I am writing a port utilization accounting tool
  13. and several points I need to pass a pointer to an element of a linked
  14. list of structures to another function.  This is being done on hp-ux
  15. 9.04 with their stock cc.  I'm using hp's xdb for debugging.  I have a
  16. function called release that rolls some numbers around.  I'm trying to
  17. pass it a pointer to the element of the linked list that I want to
  18. manipulate and from the inside of the function in question I can see
  19. that the values are all correct ant that the are updated however the
  20. address of the pointer is wrong and only the values pointed to by the
  21. pointer which is internal to this function are updated.  I call the
  22. function like this:
  23.  
  24. release(maxtime, *p); /* this p is local to the calling function and at
  25.              the time of this invocation is equal to *tty 
  26.              which is the head of the list */
  27.  
  28. The function looks like this:
  29.  
  30. release(t, p)
  31. time_t t; /* relative time passed from calling function */
  32. struct ttys *p;
  33. {
  34.   if (p->cur.unum < 1) /* no one logged in here, we really shouldn't call*/
  35.     return;           /* release if there's not a login but ...  */
  36.   p->usr[p->cur.unum].logtime += (t - p->cur.in); /* increment the
  37.                                total login time */
  38.   p->usr[p->cur.unum].logins++;
  39.   p->cur.unum = 0;
  40.   p->cur.in = 0; /* log out */
  41. }
  42.  
  43. At the end of this function I use xdb to print out the values at the other end of the pointers and get this:
  44.  
  45. >p *tty
  46. 0x400037d0  struct ttys {
  47.     line = "tty7p11";
  48.     cur = struct user {
  49.         unum = 7;
  50.         in = 823671052;
  51.     };
  52.     usr = 0x400037e4;
  53.     nxt =   00000000;
  54. }
  55. >p *p
  56. 0x7b033a2c  struct ttys {
  57.     line = "tty7p11";
  58.     cur = struct user {
  59.         unum = 7;
  60.         in = 823671052;
  61.     };
  62.     usr = 0x7b033a40;
  63.     nxt =   00000000;
  64. }
  65.  
  66. Basically *tty is untouched, *p has been but it will go away at the
  67. end of this function.  Does *p need to be global?  Should I just pass
  68. the string contained in p->line ( which should be unique ) and let the
  69. release function traverse the list searching for it?  
  70.  
  71. Your assistance and comment are appreciated.
  72.  
  73. Chuck
  74.  
  75. BTW, If any of this looks familiar the basic concept was lifted from a
  76. program called sac which runs on linux, any bugs are mine of course
  77. since the other program actually works ;->.
  78.  
  79. -- 
  80. Chuck Mattern             "That which does not kill us, makes us stronger.."
  81. cmattern@homedepot.com    -Friedrich Nietzsche-                             
  82. 2727 Paces Ferry Rd.
  83. Atlanta, GA 30339
  84.  
  85.  
  86.